 
package com.zdkj.subway.rca.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.subway.rca.entity.SwCkOthenYjzpManagementLog;
import com.zdkj.subway.rca.mapper.SwCkOthenYjzpManagementLogMapper;
import com.zdkj.subway.rca.service.SwCkOthenYjzpManagementTmpService;
import com.zdkj.subway.rca.service.SwCkOthenYjzpManagementLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * 其他运营商应急纸票导入日志表
 *
 * @author stw
 * @date 2020-12-31 19:38:11
 */
@Service
public class SwCkOthenYjzpManagementLogServiceImpl extends ServiceImpl<SwCkOthenYjzpManagementLogMapper, SwCkOthenYjzpManagementLog> implements SwCkOthenYjzpManagementLogService {
    @Autowired
    private SwCkOthenYjzpManagementTmpService swCkOthenYjzpManagementTmpService;

    @Override
    public void exportTamplateWcz( HttpServletResponse response) {
        String  verifyConfirmName = "其他运营商误出站应急纸票";
        String  tableName = "sw_ck_othen_yjzp_management_wcz";

        BufferedInputStream br = null;
        OutputStream out = null;
        try {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            final String fileName = URLEncoder.encode(verifyConfirmName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");

            br =  new BufferedInputStream(this.getClass().getResourceAsStream("/static/excel/template/import/" + tableName + ".xlsx"));

            byte[] buf = new byte[1024];
            int len = 0;

            out = response.getOutputStream();
            while ((len = br.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }
    @Override
    public void exportTamplateDkl( HttpServletResponse response) {
        String  verifyConfirmName = "其他运营商大客流应急纸票";
        String  tableName = "sw_ck_othen_yjzp_management_dkl";

        BufferedInputStream br = null;
        OutputStream out = null;
        try {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            final String fileName = URLEncoder.encode(verifyConfirmName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");

            br =  new BufferedInputStream(this.getClass().getResourceAsStream("/static/excel/template/import/" + tableName + ".xlsx"));

            byte[] buf = new byte[1024];
            int len = 0;

            out = response.getOutputStream();
            while ((len = br.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }

    @Override
    public Boolean saveBatchWithExcel(MultipartFile file) throws Exception {

        File excelFile = MultipartFileToFile.multipartFileToFile(file);
        swCkOthenYjzpManagementTmpService.saveBatchWithExcel(excelFile);

        return true;
    }

}
